package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理类")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色列表分页查询")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleVo>> selectByPage(@RequestBody RoleDto roleDto,
                                                             @PathVariable Integer pageNum,
                                                             @PathVariable Integer pageSize){
        PageResponse<RoleVo> page = roleService.selectByPage(roleDto,pageNum,pageSize);
        return success(page);
    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框列表")
    public ResponseResult<List<RoleVo>> selectList(){
        List<RoleVo> list = roleService.selectList();
        return success(list);
    }

    @PutMapping
    @ApiOperation(value = "角色添加")
    @ApiOperationSupport(includeParameters = {""})
    public ResponseResult<Boolean> addRole(@RequestBody RoleDto roleDto){
        Boolean isSuccess = roleService.addRole(roleDto);
        return success(isSuccess);
    }

    @PatchMapping
    @ApiOperation(value = "角色修改")
    @ApiOperationSupport(includeParameters = {""})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto){
        Boolean isSuccess = roleService.updateRole(roleDto);
        return success(isSuccess);
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色查询选中的资源数据")
    @ApiOperationSupport(includeParameters = {"roleDto.roleId"})
    public ResponseResult<List<String>> findCheckedResourcesByRoleId(@PathVariable Long roleId){
        List<String> list = roleService.findCheckedResourcesByRoleId(roleId);
        return success(list);
    }

    @DeleteMapping("/{roleId}")
    @ApiOperation(value = "删除角色")
    public ResponseResult deleteRole(@PathVariable String roleId){
        roleService.deleteRole(roleId);
        return success();
    }


}
